﻿Public Class ctrlThemXoaSuaCuaHang
    Private maCH, tenCH As String

    Private Sub ctrlThemXoaSuaCuaHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CapNhat()
    End Sub

    Private Sub btnThem_Click(sender As Object, e As EventArgs) Handles btnThem.Click
        Dim frm As New frmThemCuaHang
        AddHandler frm.ThemCuaHangThanhCong, AddressOf CapNhat
        frm.ShowDialog()
    End Sub

    Private Sub btnXoa_Click(sender As Object, e As EventArgs) Handles btnXoa.Click
        If CtrlDGVCuaHang1.bsCuaHang.Current IsNot Nothing Then
            If ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Bạn muốn xoá cửa hàng " + _
                                 CType(CtrlDGVCuaHang1.bsCuaHang.Current, tbCuaHang).TenCuaHang + "?" + vbNewLine + _
                                 "Mọi dữ liệu liên quan sẽ bị xoá theo.", "Xác Nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
                             = DialogResult.Yes Then
                CtrlDGVCuaHang1.bsCuaHang.RemoveCurrent()
                dt.SubmitChanges()
            End If
        End If
    End Sub

    Private Sub btnSua_CheckedChanged(sender As Object, e As EventArgs) Handles btnSua.CheckedChanged
        maCH = CtrlCuaHang1.txtMaCuaHang.Text
        tenCH = CtrlCuaHang1.txtTenCuaHang.Text
        CtrlCuaHang1.Enabled = Not CtrlCuaHang1.Enabled
    End Sub

    Private Sub btnLuuLai_Click(sender As Object, e As EventArgs) Handles btnLuuLai.Click
        CtrlCuaHang1.KryptonLabel1.Focus()
        If btnSua.Checked = False Then Exit Sub
        dt.Refresh(Data.Linq.RefreshMode.OverwriteCurrentValues, dt.tbCuaHangs)

        If CtrlCuaHang1.txtMaCuaHang.Text = "" Then
            ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Mã cửa hàng không được để trống!", "Thông Báo", _
                                                                     MessageBoxButtons.OK, MessageBoxIcon.Information)
            CtrlCuaHang1.txtMaCuaHang.Focus()
            Exit Sub
        End If

        If CtrlCuaHang1.txtMaCuaHang.Text <> maCH Then
            If dt.tbCuaHangs.Where(Function(s) s.MaCuaHang = CtrlCuaHang1.txtMaCuaHang.Text).Count > 0 Then
                ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Mã cửa hàng đã tồn tại trong hệ thống!", "Thông Báo", _
                                                                         MessageBoxButtons.OK, MessageBoxIcon.Information)
                CtrlCuaHang1.txtMaCuaHang.Focus()
                Exit Sub
            End If
        End If
        

        If CtrlCuaHang1.txtTenCuaHang.Text = "" Then
            ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Tên cửa hàng không được để trống!", "Thông Báo", _
                                                                     MessageBoxButtons.OK, MessageBoxIcon.Information)
            CtrlCuaHang1.txtTenCuaHang.Focus()
            Exit Sub
        End If

        If CtrlCuaHang1.txtTenCuaHang.Text <> tenCH Then
            ' Trùng tên cửa hàng
            If dt.tbCuaHangs.Where(Function(s) s.TenCuaHang = CtrlCuaHang1.txtTenCuaHang.Text).Count > 0 Then
                ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Tên cửa hàng đã tồn tại trong hệ thống!", "Thông Báo", _
                                                                         MessageBoxButtons.OK, MessageBoxIcon.Information)
                CtrlCuaHang1.txtTenCuaHang.Focus()
                Exit Sub
            End If
        End If

        CtrlCuaHang1.bsCuaHang.EndEdit()
        CtrlDGVCuaHang1.dgvCuaHang.EndEdit()
        dt.SubmitChanges()
        btnSua.Checked = False
    End Sub

    Private Sub txtTuKhoa_TextChanged(sender As Object, e As EventArgs) Handles txtTuKhoa.TextChanged
        If txtTuKhoa.Text = "" Then
            CapNhat()
        End If
    End Sub

    Private Sub txtTuKhoa_KeyDown(sender As Object, e As KeyEventArgs) Handles txtTuKhoa.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnTimKiem_ButtonClick(Nothing, Nothing)
        End If
    End Sub

    Private Sub btnTimKiem_ButtonClick(sender As Object, e As EventArgs) Handles btnTimKiem.ButtonClick
        Dim key As String = mdlGlobals.BoDauTiengViet(txtTuKhoa.Text.Trim.ToLower)
        If MãCửaHàngToolStripMenuItem.Checked Then
            CtrlDGVCuaHang1.bsCuaHang.DataSource = _
                dt.tbCuaHangs.Where(Function(s) dt.f_nosymbol(s.MaCuaHang).ToLower.Contains(key))
        ElseIf TênCửaHàngToolStripMenuItem.Checked Then
            CtrlDGVCuaHang1.bsCuaHang.DataSource = _
                dt.tbCuaHangs.Where(Function(s) dt.f_nosymbol(s.TenCuaHang).ToLower.Contains(key))
        End If
        btnSua.Checked = False
    End Sub

    Private Sub MãCửaHàngToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MãCửaHàngToolStripMenuItem.Click
        TênCửaHàngToolStripMenuItem.Checked = False
        MãCửaHàngToolStripMenuItem.Checked = True
    End Sub

    Private Sub TênCửaHàngToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TênCửaHàngToolStripMenuItem.Click
        TênCửaHàngToolStripMenuItem.Checked = True
        MãCửaHàngToolStripMenuItem.Checked = False
    End Sub

    Private Sub HuỷTìmKiếmToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HuỷTìmKiếmToolStripMenuItem.Click
        CapNhat()
    End Sub

    Private Sub btnQuayLai_Click(sender As Object, e As EventArgs) Handles btnQuayLai.Click
        undoChange()
        btnSua.Checked = False
        CtrlDGVCuaHang1.dgvCuaHang.Refresh()
    End Sub

    Private Sub btnCapNhat_Click(sender As Object, e As EventArgs) Handles btnCapNhat.Click
        CapNhat()
    End Sub

    Private Sub CtrlDGVCuaHang1_CuaHangThayDoi(cuaHang As tbCuaHang) Handles CtrlDGVCuaHang1.CuaHangThayDoi
        CtrlCuaHang1.CuaHang = cuaHang
    End Sub

    Private Sub CapNhat()
        CtrlDGVCuaHang1.bsCuaHang.DataSource = From ch In dt.tbCuaHangs
                                               Select ch Order By ch.TenCuaHang Ascending

        btnSua.Checked = False
    End Sub

    Private Sub CtrlDGVCuaHang1_SuaCuaHang() Handles CtrlDGVCuaHang1.SuaCuaHang
        btnSua_CheckedChanged(Nothing, Nothing)
    End Sub


    Private Sub CtrlDGVCuaHang1_ThemCuaHang() Handles CtrlDGVCuaHang1.ThemCuaHang
        btnThem_Click(Nothing, Nothing)
    End Sub

    Private Sub CtrlDGVCuaHang1_XoaCuaHang() Handles CtrlDGVCuaHang1.XoaCuaHang
        btnXoa_Click(Nothing, Nothing)
    End Sub
End Class
